﻿using NPOI.OpenXmlFormats.Dml.Diagram;

using Ookii.Dialogs.Wpf;

using PLCS.App.Shared.Extensions;
using PLCS.App.Shared.Models;
using PLCS.App.Shared.ViewModels;
using PLCS.Application.Contracts.LogShowApp.Dtos;
using PLCS.Application.Contracts.PLCReadDataApp.Dtos;
using PLCS.Client.IService.ConfigManager;
using PLCS.Client.IService.LogManager;
using PLCS.Domain.Shared.Stations;
using PLCS.Shared.Common.Constant;
using PLCS.Shared.Common.Extensions;
using PLCS.Shared.Common.Helper;

using Prism.Commands;
using Prism.Regions;

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;

namespace PLCS.App.ConfigManager.ViewModels.PLCReadData;

internal class PLCReadDataViewModel : NavigationPagedCurdViewModel<PLCReadDataDto, PLCReadDataEditDto, IPLCReadDataService>
{
    private readonly IPLCConfigService _pLCConfigService;
    private readonly IOperationLogService _logShowService;

    public DelegateCommand ImportCommand { get; set; }

    private ObservableCollection<PLCConfigViewDto> _readPlcNos;

    public ObservableCollection<PLCConfigViewDto> ReadPlcNos
    {
        get { return _readPlcNos; }
        set { SetProperty(ref _readPlcNos, value); }
    }

    private PLCConfigViewDto _selectPlcConfig;

    public PLCConfigViewDto SelectPLCConfig
    {
        get { return _selectPlcConfig; }
        set { SetProperty(ref _selectPlcConfig, value); }
    }

    private Guid _selectPLCConfigId;

    public Guid SelectPLCConfigId
    {
        get { return _selectPLCConfigId; }
        set { SetProperty(ref _selectPLCConfigId, value); }
    }

    private string _startBit;

    public string StartBit
    {
        get { return _startBit; }
        set { SetProperty(ref _startBit, value); }
    }

    private string _deviceNo;

    public string DeviceNo
    {
        get { return _deviceNo; }
        set { SetProperty(ref _deviceNo, value); }
    }

    public override async Task GetPageData(int pageIndex)
    {
        try
        {
            UpdateLoading(true);
            var res = await _service.GetListBySearchAsync((pageIndex - 1) * PageSize, PageSize, SelectPLCConfigId, StartBit, DeviceNo);
            TotalCount = (int)res.TotalCount;
            SetPageCount();
            DataGridList = res.Items.ToObservable();
        }
        finally
        {
            UpdateLoading(false);
        }
    }

    public override async Task OnNavigatedToAsync(NavigationContext navigationContext)
    {
        ReadPlcNos = new ObservableCollection<PLCConfigViewDto>(
       new[] { new PLCConfigViewDto { PLCConfigId = Guid.Empty, PLCConfigNo = "" } }
           .Concat((await _pLCConfigService.GetListAsync())
           .Select(x => new PLCConfigViewDto
           {
               PLCConfigId = x.Id,
               PLCConfigNo = x.PLCNo
           }))
            );
        await RefreshAsync();
    }

    public override bool IsNavigationTarget(NavigationContext navigationContext)
    {
        return false;
    }

    public PLCReadDataViewModel(IPLCReadDataService service, IPLCConfigService pLCConfigService, IOperationLogService logShowService) : base(service)
    {
        ImportCommand = new DelegateCommand(Import);
        _pLCConfigService = pLCConfigService;
        _logShowService = logShowService;
        View = "PLC字段配置";
    }

    private async void Import()
    {
        try
        {
            var fileDialog = new VistaOpenFileDialog();
            fileDialog.ShowDialog();
            var fileName = fileDialog.FileName;

            if (fileName == null)
                return;

            // 读取excel导入到DataTable
            var table = TableHelper.ExcelToTable(fileName);
            if (table == null) return;
            // 将DataTable转为list
            var pLCReadDataDto = table.ToList<PLCReadDataEditDto>();
            //开始导入
            await _service.CreatePLCReadDataListAsync(pLCReadDataDto);

            Log("导入", pLCReadDataDto);
            ShowMessage("导入成功");
            // 导入完刷新
            await RefreshAsync();
        }
        catch (System.Exception ex)
        {
            ShowMessage(ex.Message);
        }
    }
}

public class PLCConfigViewDto
{
    public Guid PLCConfigId { get; set; }
    public string PLCConfigNo { get; set; }
}